home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1999 January / PC Plus Super CD No55a (PCP-147A-1-99) (Disc 1) (1998).iso / ImageButton.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-07-27  |  4.2 KB  |  200 lines

  1. import java.applet.Applet;
  2. import java.applet.AudioClip;
  3. import java.awt.Color;
  4. import java.awt.Component;
  5. import java.awt.Event;
  6. import java.awt.Graphics;
  7. import java.awt.Image;
  8. import java.awt.MediaTracker;
  9. import java.awt.image.FilteredImageSource;
  10. import java.awt.image.ImageProducer;
  11. import java.net.URL;
  12.  
  13. public class ImageButton extends Applet implements Runnable {
  14.    Thread workThread;
  15.    private Image button_up;
  16.    private Image button_down;
  17.    private Image button_faded;
  18.    private AudioClip sound;
  19.    private String url;
  20.    private String status;
  21.    private String target;
  22.    private boolean isup;
  23.    private boolean isdown;
  24.    private boolean animate;
  25.    private int width;
  26.    private int height;
  27.    private int border;
  28.    private int frame;
  29.    MediaTracker tracker;
  30.  
  31.    public void stop() {
  32.       if (this.workThread != null) {
  33.          this.workThread.stop();
  34.       }
  35.  
  36.    }
  37.  
  38.    public boolean mouseEnter(Event var1, int var2, int var3) {
  39.       ((Applet)this).showStatus(this.status);
  40.       this.isup = true;
  41.       ((Component)this).repaint();
  42.       return true;
  43.    }
  44.  
  45.    public boolean mouseExit(Event var1, int var2, int var3) {
  46.       ((Applet)this).showStatus("");
  47.       this.isup = false;
  48.       this.isdown = false;
  49.       ((Component)this).repaint();
  50.       return true;
  51.    }
  52.  
  53.    public void paint(Graphics var1) {
  54.       if (this.isup) {
  55.          if (this.frame == 0) {
  56.             var1.drawImage(this.button_up, 0, 0, this);
  57.             if (this.animate) {
  58.                this.frame = 1;
  59.             }
  60.          } else {
  61.             var1.drawImage(this.button_down, 0, 0, this);
  62.             this.frame = 0;
  63.          }
  64.  
  65.          for(int var3 = 0; var3 < this.border; ++var3) {
  66.             var1.setColor(Color.lightGray);
  67.             var1.drawLine(var3, var3, this.width - var3, var3);
  68.             var1.drawLine(var3, var3, var3, this.height - var3);
  69.             var1.setColor(Color.darkGray);
  70.             var1.drawLine(this.width - var3, var3, this.width - var3, this.height - var3);
  71.             var1.drawLine(var3, this.height - var3, this.width - var3, this.height - var3);
  72.          }
  73.  
  74.       } else if (!this.isdown) {
  75.          var1.drawImage(this.button_faded, 0, 0, this);
  76.       } else {
  77.          var1.drawImage(this.button_down, 1, 1, this);
  78.  
  79.          for(int var2 = 0; var2 < this.border; ++var2) {
  80.             var1.setColor(Color.darkGray);
  81.             var1.drawLine(var2, var2, this.width - var2, var2);
  82.             var1.drawLine(var2, var2, var2, this.height - var2);
  83.             var1.setColor(Color.lightGray);
  84.             var1.drawLine(this.width - var2, var2, this.width - var2, this.height - var2);
  85.             var1.drawLine(var2, this.height - var2, this.width - var2, this.height - var2);
  86.          }
  87.  
  88.       }
  89.    }
  90.  
  91.    public boolean mouseUp(Event var1, int var2, int var3) {
  92.       this.isdown = false;
  93.       this.isup = true;
  94.       ((Component)this).repaint();
  95.       if (this.url != "") {
  96.          try {
  97.             URL var4 = new URL(((Applet)this).getCodeBase(), this.url);
  98.             ((Applet)this).getAppletContext().showDocument(var4, this.target);
  99.             return true;
  100.          } catch (Exception var5) {
  101.          }
  102.       }
  103.  
  104.       return true;
  105.    }
  106.  
  107.    public void update(Graphics var1) {
  108.       this.paint(var1);
  109.    }
  110.  
  111.    public void start() {
  112.       this.workThread = new Thread(this);
  113.       this.workThread.start();
  114.    }
  115.  
  116.    public String getAppletInfo() {
  117.       return "ImageButton ver 1.3 by Sumit Birla\n" + "http://www.csee.usf.edu/~birla\n\n";
  118.    }
  119.  
  120.    public boolean mouseDown(Event var1, int var2, int var3) {
  121.       this.isup = false;
  122.       this.isdown = true;
  123.  
  124.       try {
  125.          this.sound.play();
  126.       } catch (Exception var4) {
  127.          ((Applet)this).showStatus("Error playing sound");
  128.       }
  129.  
  130.       ((Component)this).repaint();
  131.       return true;
  132.    }
  133.  
  134.    public String getParameter(String var1, String var2) {
  135.       String var3 = ((Applet)this).getParameter(var1);
  136.       return var3 == null ? var2 : var3;
  137.    }
  138.  
  139.    public void run() {
  140.       do {
  141.          ((Component)this).repaint();
  142.  
  143.          try {
  144.             Thread.sleep(200L);
  145.          } catch (InterruptedException var1) {
  146.          }
  147.       } while(this.animate);
  148.  
  149.    }
  150.  
  151.    public void init() {
  152.       this.isup = false;
  153.       this.isdown = false;
  154.       ((Applet)this).showStatus("Loading ImageButton ver 1.3 by Sumit Birla...");
  155.       this.tracker = new MediaTracker(this);
  156.       String var1 = ((Applet)this).getParameter("button_up");
  157.       this.button_up = ((Applet)this).getImage(((Applet)this).getCodeBase(), var1);
  158.       this.tracker.addImage(this.button_up, 0);
  159.       String var2 = this.getParameter("button_down", var1);
  160.       if (var2.equals(var1)) {
  161.          this.button_down = this.button_up;
  162.       } else {
  163.          this.button_down = ((Applet)this).getImage(((Applet)this).getCodeBase(), var2);
  164.          this.tracker.addImage(this.button_down, 1);
  165.       }
  166.  
  167.       try {
  168.          this.tracker.waitForAll();
  169.       } catch (InterruptedException var6) {
  170.          ((Applet)this).showStatus("Loading interrupted");
  171.       }
  172.  
  173.       var1 = this.getParameter("grayImage", "false");
  174.       if (var1.equals("true")) {
  175.          GrayFilter var3 = new GrayFilter();
  176.          ImageProducer var4 = this.button_up.getSource();
  177.          FilteredImageSource var5 = new FilteredImageSource(var4, var3);
  178.          this.button_faded = ((Component)this).createImage(var5);
  179.       } else {
  180.          this.button_faded = this.button_up;
  181.       }
  182.  
  183.       this.url = new String(this.getParameter("url", ""));
  184.       this.status = new String(this.getParameter("status", ""));
  185.       this.target = new String(this.getParameter("target", "_self"));
  186.       var1 = this.getParameter("click_sound", "");
  187.       this.sound = ((Applet)this).getAudioClip(((Applet)this).getCodeBase(), var1);
  188.       var1 = this.getParameter("border", "1");
  189.       Integer var11 = new Integer(var1);
  190.       this.border = var11;
  191.       var1 = this.getParameter("animate", "false");
  192.       if (var1.equals("true")) {
  193.          this.animate = true;
  194.       }
  195.  
  196.       this.width = this.button_up.getWidth(this) - 1;
  197.       this.height = this.button_up.getHeight(this) - 1;
  198.    }
  199. }
  200.